package com.yan.demo.Demo9;

import java.util.ArrayList;

public class MyArrays {
    public static void main(String[] args) {
        ArrayList<Integer>list = new ArrayList<>();
        list.add(1);
        list.add(5);
        list.add(4);
        list.add(2);
        list.add(7);
        list.add(9);
        System.out.println("list = " + list);
        reverse(list);
        System.out.println("max(list) = " + max(list));
        System.out.println("min(list) = " + min(list));
        int i = indexOf(list, 7);
        System.out.println("i = " + i);
        replaceAll(list , 4,  2);
    }



    public static void reverse(ArrayList<Integer> list){
        for (int i = 0 , j = list.size() - 1; i < list.size() / 2; i++ , j--) {
                Integer temp = list.get(i);
                list.set(i , list.get(j));
                list.set(j , temp);
        }
        System.out.println(list);
    }

    public static Integer max(ArrayList<Integer> list){
        Integer Max = list.get(0);
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i) > Max){
                Max = list.get(i);
            }
        }

        return Max;
    }

    public static Integer min(ArrayList<Integer> list){
        Integer Min = list.get(0);
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i) < Min){
                Min = list.get(i);
            }
        }

        return Min;
    }

    public static int indexOf(ArrayList<Integer>list , Integer i){
        for (int j = 0; j < list.size(); j++) {
            if(list.get(j) == i){
                return j;
            }
        }
        return -1;
    }

    public static void replaceAll(ArrayList<Integer>list , Integer oldValue , Integer newValue){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i) == oldValue){
                list.set(i , newValue);
            }
        }
        System.out.println(list);
    }

}
